Prompt Bash

Posted by pulkomandy on Sun Jul 17 16:50:09 2011  •  Comments (0)  • 

This bash prompt shows up in red when the last command failed ; green if everything is fine. It also sets xterm window title to the current directory. It displays a classical user@host$ prmpt, other than that. Put this in your bashrc file to enjoy it.

#fonction exécutée avant chaque affichage du prompt
function exitstatus {
        #récupère la sortie de la dernière commande
 if [ "$?" -eq "0" ]
 then
        # 32 : vert
     PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
 else
        # 31 : rouge
     PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
 fi
    # Ajoute le changement de titre des fenêtres xterm
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\W\$\a\]$PS1"
}

PROMPT_COMMAND=exitstatus

A variant for mksh is also available (with default/red colors, but it also prints the error code!). Put this in your mkshrc file.

function precmd {
	typeset e=$?

	(( e )) && print -n "\x1B[31m$e|"
}
PS1='
$(precmd)${USER:=$(ulimit -c 0;id -un 2>&-||print \?)}@${HOSTNAME%%.*}:$(
	typeset d=${PWD:-?} n p=~; [[ $p = ?(*/) ]] || d=${d/#$p/~}
	(( ${#d} > (n = (COLUMNS/3 < 7 ? 7 : COLUMNS/3)) )) && {
	d=${d:(-n)}; p=...; } || p=; print -nr -- "$p$d") '"$PS1 "

Leave a comment

Name: Mail: